﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

public partial class DealLoan : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //从请求中获得标识ID
            string RecordId = Context.Request.Params["RecordId"];
            string ContractId = Context.Request.Params["ContractId"];
            string LoanId = Context.Request.Params["LoanId"];

            if (!string.IsNullOrEmpty(RecordId))
            {
                //预审信息业务类
                Chenxin.Lib.Business.BLL.PreliminaryBaseInfo bllPreliminary = new Chenxin.Lib.Business.BLL.PreliminaryBaseInfo();
                //买方信息业务类
                Chenxin.Lib.Business.BLL.PreBuyerInfo bllPreBuyerInfo = new Chenxin.Lib.Business.BLL.PreBuyerInfo();
                //贷款处理业务类
                //Chenxin.Lib.Business.BLL.PreLoanInfo bllPreLoanInfo = new Chenxin.Lib.Business.BLL.PreLoanInfo();

                //获取相应房源信息
                Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entPreliminary = bllPreliminary.GetModel(new Guid(RecordId));
                //买方信息
                Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1 entPreBuyer = bllPreBuyerInfo.GetMainModel(new Guid(RecordId));
                //贷款处理
                //List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo3> lsPreLoan = bllPreLoanInfo.GetModelListByMainId(RecordId);
                //Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo3 entPreLoan = bllPreLoanInfo.GetModel(new Guid(RecordId));

                //转成Json，发回客户端
                hidPreliminaryInfo.Value = Chenxin.Lib.Utility.JsonHelp.Convert2Json(entPreliminary);
                //买方信息
                hidPreBuyerInfo.Value = Chenxin.Lib.Utility.JsonHelp.Convert2Json(entPreBuyer);
                //贷款处理
                //hidPreSalerInfo.Value = Chenxin.Lib.Utility.JsonHelp.Convert2Json(lsPreLoan);
                //hidPreSalerInfo.Value = Chenxin.Lib.Utility.JsonHelp.Convert2Json(entPreLoan);
            }
            else if (!string.IsNullOrEmpty(ContractId))
            {
                //贷款处理业务类
                Chenxin.Lib.Business.BLL.PreLoanInfo bllPreLoanInfo = new Chenxin.Lib.Business.BLL.PreLoanInfo();

                //贷款处理
                Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo3 entPreLoan = new Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo3();
                entPreLoan = bllPreLoanInfo.GetModelByContractId(new Guid(ContractId));

                if (entPreLoan != null)
                {
                    //贷款处理
                    hidLoanInfo.Value = Chenxin.Lib.Utility.JsonHelp.Convert2Json(entPreLoan);
                }
                else
                {
                    Chenxin.Lib.Business.Entity.t_Contract_BaseInfo info = Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.GetModel(new Guid(ContractId));
                    entPreLoan = new Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo3();
                    entPreLoan.ContractId = info.ContractId;
                    entPreLoan.ContractCode = info.ContractCode;
                    entPreLoan.SalePrice_T = info.BargainPrice;
                    entPreLoan.Address = info.HouseAddress;
                    entPreLoan.BuildingArea = info.Area;
                    if (!string.IsNullOrEmpty(info.CompleteYear))
                    {
                        entPreLoan.CompleteYear = int.Parse(info.CompleteYear.Substring(0,4));
                    }
                    entPreLoan.CustomerName = info.BuyerName;
                    entPreLoan.IDNum = info.BuyerNo;
                    entPreLoan.Total_Floor = info.TotalFloor;
                    if (!string.IsNullOrEmpty(info.BuyerPhone))
                    {
                        entPreLoan.BuyerPhone = info.BuyerPhone;
                    }
                    else
                    {
                        entPreLoan.BuyerPhone = info.BuyerMobile;
                    }
                    if (!string.IsNullOrEmpty(info.VenderPhone))
                    {
                        entPreLoan.SalerPhone = info.VenderPhone;
                    }
                    else
                    {
                        entPreLoan.SalerPhone = info.VenderMobile;
                    }
                    entPreLoan.EmpCityId = info.BuyerCityId;
                    entPreLoan.EmpCityName = info.BuyerCityName;
                    entPreLoan.EmpCompanyId = info.BuyerCompanyId;
                    entPreLoan.EmpCompanyName = info.BuyerCompanyName;
                    entPreLoan.EmpDepartId = info.BuyerDepartId;
                    entPreLoan.EmpDepartName = info.BuyerDepartName;
                    entPreLoan.EmployeeId = info.BuyerUserId;
                    entPreLoan.EmployeeName = info.BuyerUserName;
                    hidLoanInfo.Value = Chenxin.Lib.Utility.JsonHelp.Convert2Json(entPreLoan);
                }
            }
            else if (!string.IsNullOrEmpty(LoanId))
            {
                //贷款处理业务类
                Chenxin.Lib.Business.BLL.PreLoanInfo bllPreLoanInfo = new Chenxin.Lib.Business.BLL.PreLoanInfo();

                //贷款处理
                Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo3 entPreLoan = new Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo3();
                entPreLoan = bllPreLoanInfo.GetLoanModel(new Guid(LoanId));

                if (entPreLoan != null)
                {
                    //贷款处理
                    hidLoanInfo.Value = Chenxin.Lib.Utility.JsonHelp.Convert2Json(entPreLoan);
                }
            }
            else
            {
                //Url参数出错，页面跳转
                Context.Response.Redirect(string.Format(ErrorPageUrl, Resources.Message.Err_UrlError), false);
            }
        }
        catch (Exception ex)
        {
            //Url参数出错，页面跳转
            Context.Response.Redirect(string.Format(ErrorPageUrl, ex.Message), false);
        }
    }
}
